
package com.usersmodule.dao;
import com.usersmodule.bean.TbPost;

import java.sql.*;
import java.util.*;
import util.*;
public class TbPostDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbPost tbPost=null;
    private List<TbPost> tbPostList=null;

    public boolean add(TbPost inTbPost){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Post(PostName,PostMemo) values(?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbPost.getPostName());
        pt.setString(2,inTbPost.getPostMemo());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inPostId){
        return UtilDAO.del("Tb_Post", "PostId", inPostId);
    }
    public boolean edit(TbPost inTbPost){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Post set PostId=?,PostName=?,PostMemo=?,CreateTime=getdate() where PostId=?";
        pt = conn.prepareStatement(sql);
        pt.setInt(1,inTbPost.getPostId());
        pt.setString(2,inTbPost.getPostName());
        pt.setString(3,inTbPost.getPostMemo());
        pt.setInt(4,inTbPost.getPostId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbPost getTbPostByPostId(int inPostId) {
        tbPost=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Post where PostId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inPostId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbPost == null)tbPost = getTbPost(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbPost;
    }
    public List<TbPost> getTbPostAll() {
        tbPostList=new ArrayList<TbPost>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Post";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPostList.add(getTbPost(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbPostList;
    }
    public List<TbPost> getTbPostAll(VskPage vp) {
        tbPostList=new ArrayList<TbPost>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Post";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Post", "PostId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbPostList.add(getTbPost(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
        }
        return tbPostList;
    }
    public TbPost getTbPost(ResultSet rs) throws Exception{
        TbPost tbPost = new TbPost();
        tbPost.setPostId(rs.getInt("PostId"));
        tbPost.setPostName(rs.getString("PostName"));
        tbPost.setPostMemo(rs.getString("PostMemo"));
        tbPost.setCreateTime(rs.getString("CreateTime"));
        return tbPost;
    }
}
